﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using Facebook.Rest;
using Facebook.Schema;
using Facebook.Session;
using Facebook.Utility;
using System.Linq;
using ZFCNew.Controls;
using ZFCNew.ZfcServiceReference;
using System.Net;

// TODO: Error handling
namespace ZFCNew.Code
{
    /// <summary>
    /// Sign in, Sign out, Get friends
    /// </summary>
    public partial class FacebookHelper
    {
        public static bool IsLoggedIn { get; private set; }

        public static event EventHandler LoggedIn;
        public static event EventHandler LoggedOut;
        public static event EventHandler GetMainUserCompleted;
        public static event EventHandler GetFriendsCompleted;

        public static user LoggedInUser { get; private set; }

        public static ObservableCollection<user> Friends
        {
            get;
            private set;
        }

        #region Private Members

        private static Api _fb;
        static readonly BrowserSession BrowserSession;
        private const string ApplicationKey = "121bd6ac28eabba0a6461657e12ae9c5";

        #endregion Private Members

        static FacebookHelper()
        {
            var permissions = new[]
                                  {
                                    Enums.ExtendedPermissions.publish_stream,
                                    Enums.ExtendedPermissions.read_stream,
                                    
                                  };
            BrowserSession = new BrowserSession(ApplicationKey, permissions);
            BrowserSession.LoginCompleted += LoginCompleted;
            BrowserSession.LogoutCompleted += LogoutCompleted;
        }

        #region Login/Logout

        public static void Login()
        {
            throw new NotSupportedException("Login must be done from html to avoid popup blocker issues.");
        }

        public static void Logout()
        {
            BrowserSession.Logout();
        }

        #endregion

        #region Friends

        public static void GetFriends()
        {
            _fb.Friends.GetUserObjectsAsync(new Users.GetInfoCallback(GetFriendsInfoCompleted), null);
        }

        #endregion

        #region my profile

        public void GetMyInfo()
        {
            _fb.Users.GetInfoAsync(new Users.GetInfoCallback(GetMyInfoCompleted), null);
        }

        #endregion

        #region private event handlers

        private static void LoginCompleted(object sender, EventArgs e)
        {
            _fb = new Api(BrowserSession);
            IsLoggedIn = true;
            if (LoggedIn != null)
            {
                LoggedIn(null, EventArgs.Empty);
            }

        }

        private static void LogoutCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            LoggedInUser = null;
            IsLoggedIn = false;
            if (LoggedOut != null)
            {
                LoggedOut(null, EventArgs.Empty);
            }
        }

        private static void GetMyInfoCompleted(IList<user> users, Object state, FacebookException e)
        {

            if (e == null)
            {
                LoggedInUser = users.First();
                new ServiceHelper().UpdateUser(LoggedInUser);
                if (GetMainUserCompleted != null)
                {
                    GetMainUserCompleted(null, EventArgs.Empty);
                }

                var data = new NotificationData
                               {
                                   Title = "Welcome",
                                   Message =
                                       string.Format("Welcome {0}! wish you enjoy this site ...", LoggedInUser.first_name),
                                   NotificationType = NotificationType.Error

                               };
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                                                              {
                                                                  MainPage.NotificationPop.SetMessage(data);
                                                                  MainPage.NotificationPop.Show();
                                                              });


            }

        }

        private static void GetFriendsInfoCompleted(IList<user> users, Object state, FacebookException e)
        {
            if (e == null)
            {
                Friends = users.ToObservableCollection();
                if (GetFriendsCompleted != null)
                {
                    GetFriendsCompleted(null, EventArgs.Empty);
                }

            }
            else
            {
                // TODO: error handling
            }
        }

        #endregion
    }




}
